package kr.ac.kongju.gedl.cmmn.tag;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import kr.ac.kongju.gedl.cmmn.menu.MenuLevelVO;
import kr.ac.kongju.gedl.cmmn.menu.MenuManager;
import kr.ac.kongju.gedl.cmmn.web.CU;
import kr.ac.kongju.gedl.code.vo.CodeInfoVO;

import org.apache.commons.lang.StringUtils;


public class LeftMenuTag extends TagSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = -9200870389322778179L;
	
	private String menuId;
	
	public String getMenuId() {
		return menuId;
	}

	public void setMenuId(String menuId) {
		this.menuId = menuId;
	}



	public int doStartTag() throws JspException {
		try {
			String context = pageContext.getServletContext().getContextPath();
			String url = ((HttpServletRequest)pageContext.getRequest()).getAttribute("javax.servlet.forward.request_uri").toString();
			String query = ((HttpServletRequest)pageContext.getRequest()).getQueryString();
			if(!StringUtils.isEmpty(query)) {
				url += "?"+query;
			}
			
            JspWriter jw = pageContext.getOut();
            StringBuffer sb = new StringBuffer();
            /*
            MenuManager menuManager = MenuManager.getInstance();
            MenuLevelVO currentMenu = null; 
            if(!StringUtils.isEmpty(menuId)) {
            	currentMenu = menuManager.getMenu(menuId);
            } else {
            	currentMenu = menuManager.getCurrentMenu(url);
            }
            if(currentMenu != null) {
	            MenuLevelVO topMenu = menuManager.getTopMenu(currentMenu);
	            LinkedHashMap<String, MenuLevelVO> menuList = topMenu.getSubMenulist();
	            
	            sb.append("<h2>"+topMenu.getMenuNm()+"</h2>");
	            if(menuList != null && menuList.size() > 0) {
	            	Iterator<String> key = menuList.keySet().iterator();
	            sb.append("<ul>");
	    		while(key.hasNext()) {
	    			MenuLevelVO menuLevelVO = menuList.get(key.next());
	    			
	    			String active = (menuLevelVO.getMenuId().equals(menuId) || url.indexOf(menuLevelVO.getUrl()) > -1) ? "on" : "";
	    			sb.append("<li class='"+active+"'}><a href='"+context+menuLevelVO.getUrl()+"'>"+menuLevelVO.getMenuNm()+"</a>\n");
	    			
	    			if(menuLevelVO.getSubMenulist() != null && menuLevelVO.getSubMenulist().size() > 0) {
	    				sb.append("<ul>");
	    				Iterator<String> subkey = menuLevelVO.getSubMenulist().keySet().iterator();
	    				while(subkey.hasNext()) {
	    	    			MenuLevelVO subMenuLevelVO = menuList.get(key.next());
	    	    			String subActive = (subMenuLevelVO.getMenuId().equals(menuId) || url.indexOf(subMenuLevelVO.getUrl()) > -1) ? "on" : "";
	    	    			sb.append("<li class='"+subActive+"'}><a href='"+context+subMenuLevelVO.getUrl()+"'>"+subMenuLevelVO.getMenuNm()+"</a></li>\n");
	    				}
	    				sb.append("</ul>");
	    			}
	    			sb.append("</li>\n");
	    		}
	    		sb.append("</ul>");
            }
            }
            */
            List<CodeInfoVO> regionList1 = CU.getInstance().getCodeList("REGION");
            int index = 1;
            for(CodeInfoVO codeInfo : regionList1) {
    			sb.append("<li class=\"nvparam"+index+"\"><a href=\""+menuId+"?areaCd="+codeInfo.getCodeId()+"\" >"+codeInfo.getCodeName()+"</a></li>\n");
    			index++;
    		}
            
    		jw.print(sb.toString());
    		
        } catch (Exception ex) {
            throw new JspTagException(ex.getMessage());
        }
                
        return SKIP_BODY; 
	}
	
}
